namespace SimpleWebServices.Configuration
{
    using System;
    using System.Configuration;
    using System.Xml;

    public sealed class MaintenanceConfiguration : IConfigurationSectionHandler
    {
        public static MaintenanceSettings Settings()
        {
            return MaintenanceConfiguration.Settings("simplewebservices/maintenance");
        }

        public static MaintenanceSettings Settings(string sectionName)
        {
            MaintenanceSettings result = ConfigurationManager.GetSection(sectionName) as MaintenanceSettings;

            return result ?? new MaintenanceSettings();
        }

        public object Create(object parent, object configContext, XmlNode section)
        {
            MaintenanceSettings result = null;

            if (section != null)
            {
                try
                {
                    result = section.OuterXml.Deserialize<MaintenanceSettings>();
                }
                catch (Exception exception)
                {
                    throw new ConfigurationErrorsException(exception.Message, exception, section);
                }
            }

            return result ?? new MaintenanceSettings();
        }
    }
}